requestPermissions

open fun requestPermissions(@NonNull activity: Activity, @NonNull permissions: Array<String>, @IntRange(from = 0) requestCode: Int)(source)

Requests permissions to be granted to this application. These permissions must be requested in your manifest, they should not be granted to your app, and they should have protection level dangerous, regardless whether they are declared by the platform or a third-party app.

Normal permissions PROTECTION_NORMAL are granted at install time if requested in the manifest. Signature permissions PROTECTION_SIGNATURE are granted at install time if requested in the manifest and the signature of your app matches the signature of the app declaring the permissions.

Call shouldShowRequestPermissionRationale before calling this API to check if the system recommends to show a rationale dialog before asking for a permission.

If your app does not have the requested permissions the user will be presented with UI for accepting them. After the user has accepted or rejected the requested permissions you will receive a callback reporting whether the permissions were granted or not. Your activity has to implement and the results of permission requests will be delivered to its onRequestPermissionsResult method.

Note that requesting a permission does not guarantee it will be granted and your app should be able to run without having this permission.

This method may start an activity allowing the user to choose which permissions to grant and which to reject. Hence, you should be prepared that your activity may be paused and resumed. Further, granting some permissions may require a restart of you application. In such a case, the system will recreate the activity stack before delivering the result to your onRequestPermissionsResult.

When checking whether you have a permission you should use checkSelfPermission.

Calling this API for permissions already granted to your app would show UI to the user to decide whether the app can still hold these permissions. This can be useful if the way your app uses the data guarded by the permissions changes significantly.

You cannot request a permission if your activity sets noHistory to true in the manifest because in this case the activity would not receive result callbacks including onRequestPermissionsResult.

The RuntimePermissions sample app demonstrates how to use this method to request permissions at run time.

If POST_NOTIFICATIONS is requested before the device supports the notification permission, then POST_NOTIFICATIONS will be removed from onRequestPermissionsResult. For devices that don't support POST_NOTIFICATIONS, apps can send users to its notification settings to enable notifications. See android.provider.Settings.ACTION_APP_NOTIFICATION_SETTINGS for more information on launching notification settings.

Parameters

activity

The target activity.

permissions

The requested permissions. Must be non-null and not empty.

requestCode

Application specific request code to match with a result reported to onRequestPermissionsResult. Should be >= 0.

See also